package com.sky.controller.admin;


import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类管理相关接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品及套餐分类")
    public Result addCategory(CategoryDTO categoryDTO){
        log.info("菜品及套餐分类{}",categoryDTO);
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }


    /**
     * 根据ID删除分类信息
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据ID删除信息")
    public Result deleteCategoryById(Long id){
        log.info("根据ID为{}删除信息",id);
        categoryService.deleteCategoryById(id);
        return Result.success();
    }


    /**
     * 分类的启用和禁用
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("分类的禁用和启用")
    public Result updateStartOrStop(@PathVariable Integer status, Long id){
        log.info("分类菜品的启用和禁用");
        categoryService.updateStartOrStop(status,id);
        return Result.success();
    }


    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("修改菜品的分类");
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }


    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> pageCategory(CategoryPageQueryDTO categoryPageQueryDTO){
        PageResult pageResult = categoryService.selectPageCategory(categoryPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("list")
    @ApiOperation("根据类型查询分类")
    public Result selectCategoryList(Integer type){
        log.info("根据类型查询分类");
        List<Category> list = categoryService.selectCategoryByType(type);
        return Result.success(list);
    }
}
